package com.ys.controller.web.purchase;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.purchase.TcgPurchasePayment;
import com.ys.entity.purchase.TcgPurchasePaymentItem;
import com.ys.entity.purchase.TcgPurchasePreStorage;
import com.ys.entity.purchase.TcgPurchasePreStorageSon;
import com.ys.service.flow.YspTaskService;
import com.ys.service.purchase.TcgPurchasePaymentService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2024-08-08
 * @Description: 采购付款单控制层web
 * @Version: 1.0
 */
@Controller
@RequestMapping("/web/purchase/tcgPurchasePayment")
public class WebTcgPurchasePaymentController {
    private static final Logger logger = LoggerFactory.getLogger(WebTcgPurchasePaymentController.class);


    @Autowired
    private CommonApiService<TcgPurchasePayment> commonApiService;

    private final Long MENUID = MenuId.TCG_PurchasePayment;

    @Autowired
    private YspTaskService yspTaskService;

    @Autowired
    private TcgPurchasePaymentService tcgPurchasePaymentService;


    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            String bid = commonApiService.getCommonNextBid(MENUID);
            TcgPurchasePayment tcgPurchasePreStorage = new TcgPurchasePayment()
                    .setStatus(1)
                    .setPurchasePrice(BigDecimal.ZERO)
                    .setPaidAmount(BigDecimal.ZERO)
                    .setCurrentPaymentAmount(BigDecimal.ZERO)
                    .setBid(bid)
                    .setCreateTime(new Date());

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENUID, new ArrayList<Map<String, Object>>());

            return commonApiService.toAdd(model, tcgPurchasePreStorage, MENUID, null, subGridConfig);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            if (bid == null){
                return W.error(model, "单号不能为空");
            }
            Map entity = tcgPurchasePaymentService.getExtraParam(bid);
            if (entity == null){
                return W.error(model, "采购付款单不存在!");
            }
            entity.remove("createTime");
            List<Map<String, Object>> sonlist = tcgPurchasePaymentService.getItemList(bid);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENUID,sonlist);

            //子表按钮
            Integer state = yspTaskService.flowState(bid);
            if (state == Flow.State.FINISH.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }else if (state == Flow.State.PROCESS.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }
            return commonApiService.toEdit(model,Long.parseLong(entity.get("id").toString()), new TcgPurchasePayment(), MENUID, entity, subGridConfig);
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }
}
